//
//  ZH_MallHotTableViewCell.m
//  WisdomStar
//
//  Created by zhangliwen on 2018/10/19.
//  Copyright © 2018年 zhangliwen. All rights reserved.
//

#import "ZH_MallHotTableViewCell.h"
#import "ZH_MallHotCollectionViewCell.h"
@interface ZH_MallHotTableViewCell()<UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout>
@property (nonatomic,weak)UIView *bgView;
@property (nonatomic,weak)UIImageView *hotImage;
@property (nonatomic,weak)UILabel *hotLabel;
@property (nonatomic,weak)UIButton *moreButton;
@property (nonatomic,weak)UIView *lineView;
///内容
@property(nonatomic, weak) UICollectionView *collectionView;
///数据
@property(nonatomic, strong) NSMutableArray *dataArray;
@end
@implementation ZH_MallHotTableViewCell

///创建
+(instancetype)cellWithTableView:(UITableView*)tableView
{
    static NSString *ID = @"ZH_MallHotTableViewCell";
    ZH_MallHotTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    if(cell == nil){
        cell = [[ZH_MallHotTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
    }
    return cell;
}
///初始化
-(void)initDefault
{
    [self unShowClickEffect];
}
- (void)loadSubviewConstraints{
    [self.bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.equalTo(self.contentView);
        make.left.equalTo(self.contentView).offset(FITSCALE(12));
        make.right.equalTo(self.contentView).offset(FITSCALE(-12));
//        make.height.mas_equalTo(FITSCALE(204));
    }];
    [self.hotImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.bgView).offset(FITSCALE(11));
        make.top.equalTo(self.bgView).offset(FITSCALE(10));
    }];
    [self.hotLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.hotImage);
        make.left.equalTo(self.hotImage.mas_right).offset(FITSCALE(7));
    }];
    [self.moreButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.hotLabel);
        make.right.equalTo(self.bgView).offset(FITSCALE(-13));
    }];
    [self.lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.hotImage.mas_bottom).offset(FITSCALE(9));
        make.left.equalTo(self.bgView).offset(FITSCALE(10));
        make.right.equalTo(self.bgView).offset(FITSCALE(-10));
    }];
    [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.lineView.mas_bottom);
        make.left.equalTo(self.bgView).offset(FITSCALE(10));
        make.right.equalTo(self.bgView).offset(FITSCALE(-10));
        make.bottom.equalTo(self.bgView).offset(FITSCALE(-11));
    }];
}
///更新 ui
- (void)updateUiForData:(NSMutableArray *)datas
{
    if(datas.count > 0){
        [self.dataArray removeAllObjects];
        [self.dataArray addObjectsFromArray:datas];
        
        NSInteger item = self.dataArray.count;
        NSInteger rowNum = (item / 3) + ((item % 3) > 0 ? 1 : 0);
        
        CGFloat collectionHeight = FITSCALE(167) * rowNum + (rowNum * FITHEIGHT(0));
        
        [self.collectionView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.height.mas_equalTo(collectionHeight);
        }];
        
        [self.collectionView reloadData];
    }else{
        [self.collectionView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.height.mas_equalTo(HEIGHT_TRANSFORMATION(0));
        }];
    }
}
#pragma mark -- UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    
//    if(self.onClickBlock){
//        self.onClickBlock(self.dataArray[indexPath.row][1]);
//    }
    
    [self.collectionView reloadData];
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.dataArray.count;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    CGSize size = CGSizeMake((kMainScreenWidth - FITSCALE(58)) / 3, FITSCALE(174/2));
    return size;
}

///设置行间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
{
    return FITHEIGHT(0);
}

///设置列间距，配合item宽度来设置
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
{
    return WIDTH_TRANSFORMATION(0);
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    ZH_MallHotCollectionViewCell *cell = [ZH_MallHotCollectionViewCell collectionView:collectionView cellForItemAtIndexPath:indexPath];
    [cell updaUI:self.dataArray[indexPath.row]];
    return cell;
}



#pragma mark -- getter,setter

///内容
- (UICollectionView *)collectionView
{
    if(!_collectionView){
        UICollectionView *collectionView = [UICollectionView createCollectionViewWithDelegateTarget:self forScrollDirection:UICollectionViewScrollDirectionVertical];
        //        collectionView.pagingEnabled = YES;
        [self.contentView addSubview:_collectionView = collectionView];
        //注册
        [ZH_MallHotCollectionViewCell registerCollectionView:collectionView];
    }
    return _collectionView;
}


///数据
- (NSMutableArray *)dataArray
{
    if(!_dataArray){
        _dataArray = [[NSMutableArray alloc] initWithCapacity:0];
    }
    return _dataArray;
}

#pragma mark - getter,setter
- (UIView *)bgView{
    if (!_bgView) {
        UIView *view = [UIView createViewWithBackgroundColor:kCommonWhiteBg];
        view.layer.cornerRadius = FITSCALE(3);
        view.layer.masksToBounds = YES;
        view.clipsToBounds = YES;
        [self.contentView addSubview:_bgView = view];
    }
    return _bgView;
}
- (UIImageView *)hotImage{
    if (!_hotImage) {
        UIImageView *image = [UIImageView createImageViewWithName:@"icon_hot"];
        [self.contentView addSubview:_hotImage = image];
    }
    return _hotImage;
}
- (UILabel *)hotLabel{
    if (!_hotLabel) {
        UILabel *label = [UILabel createLabelWithText:@"热销榜" font:kFontSize_24];
        label.font = [UIFont boldSystemFontOfSize:14];
        [self.contentView addSubview:_hotLabel = label];
    }
    return _hotLabel;
}
- (UIButton *)moreButton{
    if (!_moreButton) {
        UIButton *button = [UIButton createNoBgButtonWithTitle:@"更多" target:self action:nil];
        [button setTitleColor:[UIColor colorWithHexString:@"#999999"] forState:UIControlStateNormal];
        [self.contentView addSubview:_moreButton = button];
    }
    return _moreButton;
}
- (UIView *)lineView{
    if (!_lineView) {
        UIView *view = [UIView createViewWithBackgroundColor:[UIColor colorWithHexString:@"#DFDFDF"]];
        [self.contentView addSubview:_lineView = view];
    }
    return _lineView;
}
@end
